package org.ngbx.demo.flink.clickevent.mocker;

import org.ngbx.demo.flink.clickevent.UserClickedEvent;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

public class UserClickedEventIterator {
    private int nextUserIdIndex;
    public static List<Long> userIdList;
    private static Long lastEventTime;

    UserClickedEventIterator() {
        // nextTimestampPerKey = new HashMap<>();
        this.nextUserIdIndex = 0;
    }

    UserClickedEvent next() {
        Long userId = nextUserId();
        return new UserClickedEvent(userId, nextTimestamp());
    }

    private Long nextTimestamp() {
        if (lastEventTime == null) {
            lastEventTime = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        } else {
            // 自动增加1秒
            lastEventTime = lastEventTime + 1000;
        }
        return lastEventTime;
    }

    private Long nextUserId() {
        Long nextUserId = userIdList.get(this.nextUserIdIndex);
        if (this.nextUserIdIndex == userIdList.size() - 1) {
            this.nextUserIdIndex = 0;
        } else {
            this.nextUserIdIndex++;
        }
        return nextUserId;
    }
}
